package cn.wjx.taobaounion.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.ui.custom.LoadingView;

/**
 * @author WuChangJian
 * @date 2020/6/16 21:10
 */
public abstract class BaseFragment extends Fragment {

    private Unbinder mBind;
    private FrameLayout mBaseContainer;
    private View mSuccessView;
    private View mLoadingView;
    private View mErrorView;
    private View mEmptyView;
    private State currentState = State.NONE;
    protected enum State {
        NONE, SUCCESS, ERROR, EMPTY, LOADING
    }
    @BindView(R.id.loading_view)
    public LoadingView mRealLoadingView;
    @OnClick(R.id.reload)
    public void reload() {
        onClickReload();
    }

    /**
     * 网络错误点击重试
     * 子类可有选择的实现
     */
    protected void onClickReload() {}

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = loadRootView(inflater, container);
        mBaseContainer = rootView.findViewById(R.id.fragment_base_container);
        loadStatesView(inflater, container);   // 先将子布局载入，再去用黄油刀
        mBind = ButterKnife.bind(this, rootView);
        initView(rootView);
        initListener();
        initPresenter();
        loadData();
        return rootView;
    }

    /**
     * 如果子类有监听事件，覆盖此方法即可
     */
    protected void initListener() {

    }

    /**
     * 默认情况下的加载的布局根布局
     * @param inflater
     * @param container
     * @return
     */
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_base, container, false);
    }

    /**
     * 加载各种状态的View
     * @param inflater
     * @param container
     */
    private void loadStatesView(LayoutInflater inflater, ViewGroup container) {
        // 加载成功的页面
        mSuccessView = loadSuccessView(inflater, container);
        mBaseContainer.addView(mSuccessView);
        // 正在加载的页面
        mLoadingView = loadLoadingView(inflater, container);
        mBaseContainer.addView(mLoadingView);
        // error
        mErrorView = loadErrorView(inflater, container);
        mBaseContainer.addView(mErrorView);
        // 加载数据为空
        mEmptyView = loadEmptyView(inflater, container);
        mBaseContainer.addView(mEmptyView);
        // 初始都设置为不可见
        setUpState(State.NONE);
    }

    protected abstract int getRootViewResId();


    protected void initView(View rootView) {

    }

    protected void loadData() {

    }

    protected void initPresenter() {

    }

    /**
     * 切换页面状态
     * 子类所调用
     * @param state
     */
    protected void setUpState(State state) {
        if (mRealLoadingView != null) {
            if (state != State.LOADING) {
                mRealLoadingView.setVisibility(View.GONE);
            }
        }
        this.currentState = state;
        mLoadingView.setVisibility(currentState == State.LOADING ? View.VISIBLE : View.GONE);
        mSuccessView.setVisibility(currentState == State.SUCCESS ? View.VISIBLE : View.GONE);
        mErrorView.setVisibility(currentState == State.ERROR ? View.VISIBLE : View.GONE);
        mEmptyView.setVisibility(currentState == State.EMPTY ? View.VISIBLE : View.GONE);
    }

    /**
     * 加载Loading界面
     * @param inflater
     * @param container
     * @return
     */
    protected View loadLoadingView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_loading, container, false);
    }

    protected View loadSuccessView(LayoutInflater inflater, ViewGroup container) {
        int resId = getRootViewResId();
        return inflater.inflate(resId, container, false);
    }

    protected View loadErrorView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_error, container, false);
    }

    protected View loadEmptyView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_empty, container, false);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mBind != null) {
            mBind.unbind();
        }
        release();
    }

    protected void release() {

    }
}
